package com.fx.eduorder.controller;


import com.fx.commonuntils.ErrorEnum;
import com.fx.commonuntils.ReturnMessageUtils;
import com.fx.eduorder.service.PayLogService;
import com.fx.commonuntils.ReturnMessage;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-11-16
 */
@RestController
@RequestMapping("/eduorder/paylog")
@CrossOrigin
@Api(description = "订单二维码管理系统")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class PayLogController {


    final private PayLogService payLogService;

    //生成微信支付二维码接口
    //参数是订单号
    @GetMapping("createNative/{orderNo}")
    public ReturnMessage<Object> createNative(@PathVariable String orderNo) {
        //返回信息，包含二维码地址，还有其他需要的信息


        return payLogService.createNatvie(orderNo);
    }

    //查询订单支付状态
    //参数：订单号，根据订单号查询 支付状态
    @GetMapping("queryPayStatus/{orderNo}")
    public ReturnMessage<Object> queryPayStatus(@PathVariable String orderNo) {
        Map<String,String> map = payLogService.queryPayStatus(orderNo);
        System.out.println("*****查询订单状态map集合:"+map);
        if(map == null) {
            //return R.error().message("支付出错了");
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_ACTIVIT_DEFINITION_GET);
        }
        //如果返回map里面不为空，通过map获取订单状态
        if(map.get("trade_state").equals("SUCCESS")) {//支付成功
            //添加记录到支付表，更新订单表订单状态
            payLogService.updateOrdersStatus(map);
            return ReturnMessageUtils.success("支付成功");
        }
        return ReturnMessageUtils.errorWithEum(ErrorEnum.E_UCORDER_PHNOE_PAY);

    }
}



